package com.zmn.oms.services.impl.orderpay;

import com.alibaba.fastjson.JSON;
import com.zmn.account.common.constant.IdentityConsts;
import com.zmn.account.common.dto.account.AccountInfoDIO;
import com.zmn.account.common.dto.account.AccountInfoDRO;
import com.zmn.account.dubbo.interfaces.account.AccountListRemoteService;
import com.zmn.account.dubbo.utils.AccountDubboConsts;
import com.zmn.common.constant.FicoConsts;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.factory.OrderFactory;
import com.zmn.oms.services.interfaces.factory.OrderFactoryService;
import com.zmn.oms.services.interfaces.orderpay.AccountPayBService;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;

import static com.zmn.erp.common.constrants.ErpGlobalConstrants.SYSTEM_USER;
import static com.zmn.erp.common.constrants.ErpGlobalConstrants.SYSTEM_USER_ID;

/**
 * 类描述：工单支付渠道
 *
 * @author heciqi
 * @date 2020/09/28 15:19
 */
@Slf4j
@Service
public class AccountPayBServiceImpl implements AccountPayBService {
	@Autowired
	private OrderFactoryService orderFactoryService;
//	@Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
//	private FactoryListRemoteService factoryListRemoteService;
	@Reference(version = AccountDubboConsts.INTERFACE_VERSION, check = false)
	private AccountListRemoteService accountListRemoteService;

	/**
	 * 检测余额
	 *
	 * @param orderId
	 */
	@Override
	public void checkAccount(Long orderId, Integer payAmount) throws OmsBaseException {
		// 获取厂商单信息
		OrderFactory orderFactory = orderFactoryService.findOrderFactoryByKey(orderId);
		if (orderFactory == null || NumberUtil.isNullOrZero(orderFactory.getFactoryId())) {
			log.error("[{}]未找到厂商用户", orderId);
			return;
		}

		this.checkAccount(orderFactory.getFactoryId(), payAmount, orderFactory.getFreezeAmount());
	}

	/**
	 * 检测余额
	 *
	 * @param factoryId
	 * @param payAmount
	 */
	@Override
	public void checkAccount(Integer factoryId, Integer payAmount) throws OmsBaseException {
		this.checkAccount(factoryId, payAmount, null);
	}

	/**
	 * 检测余额
	 *
	 * @param factoryId
	 * @param payAmount
	 */
	private void checkAccount(Integer factoryId, Integer payAmount, Integer freezeAmount) throws OmsBaseException {
		/*FactoryBaseDRO factoryBaseDRO = this.getFactoryInfo(factoryId);

		AccountInfoDRO accountInfoDRO = this.getAccountInfoByFactoryInfo(factoryBaseDRO);

		if (accountInfoDRO == null) {
			throw new OmsBaseException("未找到资金账户");
		}

		// 检测余额
		// 获取本单冻结金额
		Integer balanceTotalAmount = Optional.ofNullable(accountInfoDRO.getBalanceAmount()).orElse(0)
				+ Optional.ofNullable(freezeAmount).orElse(0);
		if (balanceTotalAmount < payAmount) {
			log.error("[{}]消费金额[{}]余额[{}]不足", factoryId, payAmount, balanceTotalAmount);
			if (Objects.equals(factoryBaseDRO.getSettlementType(), 2)) {
				throw new OmsBaseException("账户授信余额不足");
			} else {
				throw new OmsBaseException("账户余额不足");
			}
		}*/
	}

	/**
	 * 根据订单id获取资金账户
	 *
	 * @param orderFactory
	 * @return
	 */
	@Override
	public AccountInfoDRO getAccountInfoByOrderId(OrderFactory orderFactory) {
		return null;
		/*if (orderFactory == null || NumberUtil.isNullOrZero(orderFactory.getFactoryId())) {
			log.error("[{}]未找到厂商信息", orderFactory.getFcOrderId());
			return null;
		}

		FactoryBaseDRO factoryBaseDRO = this.getFactoryInfo(orderFactory.getFactoryId());

		// 查询厂商类型
		Integer settlementType = getFactoryInfo(orderFactory.getFactoryId()).getSettlementType();

		if (NumberUtil.isNullOrZero(settlementType)) {
			log.error("[{}]厂商结算类型获取失败", orderFactory.getFactoryId());
			return null;
		}

		// 默认预充值：余额账户
		Integer accountCategId = com.zmn.account.common.constant.AccountConsts.ACCOUNT_CATEG_TRADE_FUND;
		if (Objects.equals(factoryBaseDRO.getSettlementType(), 2)) {
			accountCategId = com.zmn.account.common.constant.AccountConsts.ACCOUNT_CATEG_CREDIT;
		}
		AccountInfoDIO accountInfo = new AccountInfoDIO();
		// 余额账户
		accountInfo.setAccountCategId(accountCategId);
		// 服务商类型
		accountInfo.setIdentityId(IdentityConsts.ACCOUNT_IDENTITY_ID_FACTORY);
		// 服务商ID
		accountInfo.setIdentityObjectId(factoryBaseDRO.getFcId().longValue());
		accountInfo.setOperator(SYSTEM_USER);
		accountInfo.setOperatorId((long) SYSTEM_USER_ID);
		accountInfo.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

		// 查询厂商账户信息
		com.zmn.common.dto2.ResponseDTO<AccountInfoDRO> resp = accountListRemoteService.getAccountInfo(accountInfo);
		if (!resp.isSuccess()) {
			log.error("[{}]未找到资金账户，原因[{}]", JSON.toJSONString(accountInfo), resp.getMessage());
			return null;
		}
		return resp.getData();*/
	}

	/**
	 * 根据厂商信息获取资金账户
	 *
	 * @param factoryBaseDRO
	 * @return
	 */
	/*private AccountInfoDRO getAccountInfoByFactoryInfo(FactoryBaseDRO factoryBaseDRO) {
		// 查询厂商类型
		Integer settlementType = factoryBaseDRO.getSettlementType();

		if (NumberUtil.isNullOrZero(settlementType)) {
			log.error("[{}]厂商结算类型获取失败", JSON.toJSONString(factoryBaseDRO));
			return null;
		}

		// 默认预充值：余额账户
		Integer accountCategId = com.zmn.account.common.constant.AccountConsts.ACCOUNT_CATEG_TRADE_FUND;
		if (Objects.equals(factoryBaseDRO.getSettlementType(), 2)) {
			accountCategId = com.zmn.account.common.constant.AccountConsts.ACCOUNT_CATEG_CREDIT;
		}
		AccountInfoDIO accountInfo = new AccountInfoDIO();
		// 余额账户
		accountInfo.setAccountCategId(accountCategId);
		// 服务商类型
		accountInfo.setIdentityId(IdentityConsts.ACCOUNT_IDENTITY_ID_FACTORY);
		// 服务商ID
		accountInfo.setIdentityObjectId(factoryBaseDRO.getFcId().longValue());
		accountInfo.setOperator(SYSTEM_USER);
		accountInfo.setOperatorId((long) SYSTEM_USER_ID);
		accountInfo.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);

		// 查询厂商账户信息
		com.zmn.common.dto2.ResponseDTO<AccountInfoDRO> resp = accountListRemoteService.getAccountInfo(accountInfo);
		if (!resp.isSuccess()) {
			log.error("[{}]未找到资金账户，原因[{}]", JSON.toJSONString(accountInfo), resp.getMessage());
			return null;
		}
		return resp.getData();
	}*/

//	private FactoryBaseDRO getFactoryInfo(Integer factoryId) {
//		com.zmn.common.dto2.ResponseDTO<FactoryBaseDRO>  factoryBaseDROResponseDTO = factoryListRemoteService.getFactory(factoryId);
//		FactoryBaseDRO factoryBaseDRO = factoryBaseDROResponseDTO.getData();
//		if (!factoryBaseDROResponseDTO.isSuccess() || factoryBaseDRO == null) {
//			log.error("[{}]未找到厂商信息，原因[{}]", factoryId, factoryBaseDROResponseDTO.getMessage());
//			return new FactoryBaseDRO();
//		}
//		return factoryBaseDRO;
//	}

	/**
	 * 获取资金帐号支付渠道
	 * @param orderId
	 * @return
	 */
	@Override
	public Integer getPayChannelIdByOrderId(Long orderId) {

		OrderFactory orderFactory = orderFactoryService.findOrderFactoryByKey(orderId);
		if (Objects.isNull(orderFactory) || Objects.isNull(orderFactory.getFactoryId())) {
			log.error("[{}]未找到厂商信息", orderId);
			return null;
		}
		return this.getPayChannelIdByFactoryId(orderFactory.getFactoryId());
	}

	/**
	 * 获取资金帐号支付渠道
	 * @param factoryId
	 * @return
	 */
	@Override
	public Integer getPayChannelIdByFactoryId(Integer factoryId) {
		return null;
	/*	FactoryBaseDRO factoryBaseDRO = this.getFactoryInfo(factoryId);
		if (Objects.isNull(factoryBaseDRO)) {
			return null;
		}
		Integer settType = factoryBaseDRO.getSettlementType();
		Integer payChannelId = null;
		if (Objects.equals(FicoConsts.FACTORY_ACCOUNT_TYPE_PRECHARGE, settType)) {
			payChannelId = PayConsts.PAY_CHANNEL_PLATFORM_BALANCEAMOUNT;
		}
		// 后结算-授信支付
		else if (Objects.equals(FicoConsts.FACTORY_ACCOUNT_TYPE_SETTLEMENT, settType)) {
			payChannelId = PayConsts.PAY_CHANNEL_PLATFORM_CREDIT;
		} else {
			log.error("[{}]未知结算类型", settType);
		}
		return payChannelId;*/
	}
}
